Imports System.Data
Imports System.Data.SqlClient
Partial Class Client_frmShowCart
    Inherits System.Web.UI.Page
    Dim _UserId As String
    Dim _BO As New Bo

    Dim _ShipId As String
    Dim _Charge As Integer
    Dim _AddToCartVo As New AddToCartVo
    Dim _NetPayableAmount As Integer
    Dim _AddToCartMasterVo As New AddToCartMasterVo
    Dim _ProductVo As New ProductVo
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)
        _UserId = Session("UserId")
        If Not Page.IsPostBack Then
            If _UserId = "" Then
                Response.Redirect("frmLogin.aspx")
            Else
                GridData()
            End If
        End If
    End Sub
    Function ShipCharge(ByVal _DiscountedPrice As String) As String
        Dim ShipId As String
        If Val(_DiscountedPrice) < 2000 Then
            ShipId = "s1"
        ElseIf Val(_DiscountedPrice) < 4000 Then
            ShipId = "s2"
        ElseIf Val(_DiscountedPrice) < 6000 Then
            ShipId = "s3"
        ElseIf Val(_DiscountedPrice) < 8000 Then
            ShipId = "s4"
        ElseIf Val(_DiscountedPrice) < 10000 Then
            ShipId = "s5"
        Else
            ShipId = "s6"
        End If
        Return ShipId
    End Function
    Protected Sub dd_RowEditing(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        gdAddToCart.EditItemIndex = e.Item.ItemIndex
        GridData()

    End Sub
    Protected Sub dd_RowCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        gdAddToCart.EditItemIndex = -1
        GridData()

    End Sub
    Protected Sub dd_RowUpdate(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Dim s As String
        s = CType(e.Item.FindControl("tt"), TextBox).Text

        Dim _AvailableQuantity As Integer
        Dim TotalQuantity As Integer

        _ProductVo = _BO.getProductById(e.Item.Cells(1).Text)
        _AvailableQuantity = Integer.Parse(_ProductVo.AvailableQuantity)
        TotalQuantity = (Integer.Parse(s))
        If _AvailableQuantity < TotalQuantity Then
            MsgBox("Quantity Not In Stock ")
        Else
            _BO.UpdateQuantityInCart(_UserId, e.Item.Cells(1).Text, s)

        End If

        ' _BO.UpdateQuantityInCart(_UserId, e.Item.Cells(1).Text, s)
        gdAddToCart.EditItemIndex = -1
        '_BO.setQty()
        GridData()
        'Call calculate()
    End Sub
    Sub GridData()
        Dim dtAddToCart As DataTable
        dtAddToCart = _BO.getcart(_UserId)
        If dtAddToCart.Rows.Count = 0 Then
            MsgBox("No Items In Your Bag")
            Response.Redirect("frmDataList.aspx")
        End If
        gdAddToCart.DataSource = dtAddToCart
        gdAddToCart.DataBind()
        Call calculate_total()
    End Sub
    Protected Sub gdAddToCart_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles gdAddToCart.DeleteCommand
        _BO.DeleteFromCart(_UserId, e.Item.Cells(1).Text)
        GridData()
        Response.Redirect("frmShowCart.aspx")
    End Sub

    Protected Sub lnkMakePayment_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkMakePayment.Click
        _UserId = Session("UserId")
        If Not Page.IsPostBack Then
            If _UserId = "" Then
                Response.Redirect("frmLogin.aspx")
            End If
        End If

        _AddToCartMasterVo.UserId = _UserId
        _AddToCartMasterVo.TotalQuantity = lblTotalQuantity.Text
        _AddToCartMasterVo.TotalPrice = (Val(lblDiscountedPrice.Text) * Val(lblTotalQuantity.Text))
        _BO.AddToCartMaster(_AddToCartMasterVo)
        Session("UserId") = _UserId
        Session("DiscountedPrice") = lblDiscountedPrice.Text
        Session("TotalQuantity") = lblTotalQuantity.Text
        Response.Redirect("frmCreditCard.aspx")
    End Sub
    Sub calculate_total()
        Dim dt As New DataTable
        Dim dt1 As New DataTable

        
        dt = _BO.getTotal(_UserId)
        lblTotalQuantity.Text = dt.Rows(0).Item(0).ToString
        lblDiscountedPrice.Text = dt.Rows(0).Item(1).ToString
        lblOriginalPrice.Text = dt.Rows(0).Item(2).ToString


    

        _ShipId = ShipCharge(lblDiscountedPrice.Text)
        dt1 = _BO.getShippingCharge(_ShipId)
        If dt1.Rows.Count <> 0 Then
            _Charge = ((Val(dt1.Rows(0).Item(1).ToString) * Val(lblDiscountedPrice.Text)) / 100)
            _NetPayableAmount = _Charge + Val(lblDiscountedPrice.Text)
            lblShippingCharge.Text = _Charge
            lblNetPayableAmount.Text = _NetPayableAmount
            Session.Add("netAmount", _NetPayableAmount)
        End If
    End Sub
    Protected Sub lnkContinueShopping_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkContinueShopping.Click
        Response.Redirect("frmDataList.aspx")
    End Sub

    Protected Sub gdAddToCart_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles gdAddToCart.EditCommand
        'Session("UserId") = _UserId
        'Response.Redirect("frmEditQuantity.aspx?ProductId=" & e.Item.Cells(1).Text)
    End Sub

    Protected Sub gdAddToCart_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles gdAddToCart.PageIndexChanged
        gdAddToCart.CurrentPageIndex = e.NewPageIndex
        GridData()
    End Sub
    'Sub calculate()
    '    Dim dt As New DataTable
    '    Dim dr As DataRow
    '    Dim Tot_qty As Integer = 0
    '    Dim totofqty As Integer = 0
    '    Dim Tot_discPrice As Integer = 0
    '    Dim Tot_origPrice As Integer = 0
    '    Dim ActualPrice As Integer = 0
    '    Dim NetPayableAmount As Integer = 0
    '    Dim dt1 As New DataTable
    '    dt = _BO.getCartDetails(_UserId)
    '    For Each dr In dt.Rows
    '        Tot_qty = Val(dt.Rows().Item(0).ToString)
    '        totofqty = totofqty + (Val(dr("Quantity")))
    '        Tot_discPrice = Tot_discPrice + Val(dr("DiscountedPrice"))
    '        Tot_origPrice = Tot_origPrice + Val(dr("OriginalPrice"))
    '        ActualPrice = ActualPrice + (Tot_qty * Tot_origPrice)
    '        NetPayableAmount = NetPayableAmount + (Tot_qty * Tot_discPrice)
    '    Next
    '    lblTotalQuantity.Text = totofqty
    '    lblDiscountedPrice.Text = NetPayableAmount
    '    lblOriginalPrice.Text = ActualPrice

    '    _ShipId = ShipCharge(lblDiscountedPrice.Text)
    '    dt1 = _BO.getShippingCharge(_ShipId)
    '    If dt1.Rows.Count <> 0 Then
    '        _Charge = ((Val(dt1.Rows(0).Item(1).ToString) * Val(lblDiscountedPrice.Text)) / 100)
    '        _NetPayableAmount = _Charge + Val(lblDiscountedPrice.Text)
    '        lblShippingCharge.Text = _Charge
    '        lblNetPayableAmount.Text = _NetPayableAmount
    '    End If


    'End Sub
End Class

